﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.RoleManage
{
    public partial class Modify : Common.PageBase
    {
        T_ROLE_BLL bll = new T_ROLE_BLL();
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);           
            //PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlMOBILEUSERTYPEBinding();
                ddlVIEWRIGHTBinding();
                ddlBUSINESSDIVISIONIDBinding();
                ddlSTATUSBinding();
                pageDataInit();
            }
        }

       

        #region 页面初始化数据绑定

        private void ddlMOBILEUSERTYPEBinding()
        {
            ddlMOBILEUSERTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlMOBILEUSERTYPE, typeof(MobileUserType));
        }

        private void ddlVIEWRIGHTBinding()
        {
            ddlVIEWRIGHT.Items.Clear();
            EnumHelper.FillListControl(ddlVIEWRIGHT, typeof(ViewRight));
        }

        private void ddlSTATUSBinding()
        {
            ddlSTATUS.Items.Clear();
            EnumHelper.FillListControl(ddlSTATUS, typeof(Status));   
        }
        private void ddlBUSINESSDIVISIONIDBinding()
        {
            ddlBUSINESSDIVISIONID.DataSource = bdBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONID.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONID.DataValueField = "BUSINESSDIVISIONID";
            ddlBUSINESSDIVISIONID.DataBind();

        }


        


        #endregion

        private void pageDataInit()
        {
            string _roleid = String.IsNullOrEmpty(Request.QueryString["ROLEID"]) ? "" : Request.QueryString["ROLEID"].Trim();

            if (!String.IsNullOrEmpty(_roleid))
            {
                T_ROLE_Entity entity = bll.GetEntity("ROLEID", _roleid);

                PageHelper.SetControlValue<T_ROLE_Entity>(this.Page, entity);
                lblTitle.Text = "角色修改";
            }
            else
            {
                lblTitle.Text = "角色添加";
            }
            txtCREATEPEOPLE.Enabled = false;
            txtCREATEPEOPLEID.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            txtLASTMODIFYPEOPLEID.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;
            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _roleid = String.IsNullOrEmpty(Request.QueryString["ROLEID"]) ? "" : Request.QueryString["ROLEID"].Trim();


            T_ROLE_Entity entity = bll.GetEntity("ROLEID", _roleid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_ROLE_Entity>(this.Page, ref entity);
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now; 
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                try
                {

                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_ROLE_Entity();

                PageHelper.GetControlValue<T_ROLE_Entity>(this.Page, ref entity);
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                try
                {

                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }

            }
        }
    }
}
